
var netUtil = require('/../../utils/netUtil.js')
var util = require('/../../utils/util.js')
var app = getApp();

Page({
  
  data:{
  		comboArray : getApp().globalData.comboArray ,
			accountPayType : null,
 			accountCode : null,
 			accountStorageQueryResult : null   //查询仓租明细的返回结果
 		
  },
  onLoad:function(options){
  	
  	var accountPayType = options.accountPayType;
  	var accountCode = options.accountCode;
  	
  	if(accountPayType != undefined && accountCode != undefined) {
  		this.setData({
  			accountPayType : accountPayType,
  			accountCode : accountCode
  		});
  	}
  	
  	//缴费通知 只有仓租费用，没有出入仓费用，不用考虑
  	app.getUserOpedId(this.getAccountStorageQueryResult);
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
   //查询仓租明细的返回结果
  getAccountStorageQueryResult : function() {
  		var that = this;
  		var params = {
  			method : 'wechat.wisen.account.storage.query',
  			openId : app.globalData.userInfo.openid,
  			data : {
  					accountCode : that.data.accountCode
  				}
  		};
  		
  			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  				onPre: function() {
						netUtil.showLoadingDialog(that);
					},
					onSuccess : function(resp) {
						netUtil.hideLoadingDialog(that);
						
						//如果请求成功
						if(resp.ret == app.globalData.respSuccessCode) {
						
							var data = resp.data;
							that.setData({
								accountStorageQueryResult : data
							});
						
						} else {
							if(resp.message != undefined && resp.message != null && resp.message != '') {
 								netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 							} else {
 								netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 							}
						}
					},
					onError: function(msgCanShow, code, hiddenMsg) {
        		console.log("fail");
						netUtil.hideLoadingDialog(that);
						netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
					}
  		}).send();
  },
  
  //选择套餐
	chooseCombo: function(e) {
	 	
	 	
	 	var that = this;
   	
   	var value = e.detail.value;  //选中的数组的下表
   	var initemsindex = e.currentTarget.dataset.initemsindex;
	
		var chargeType = that.data.comboArray[value].comboKey;
		
	/*	if(chargeType == 'A') {
			   	that.data.accountStorageQueryResult.items[initemsindex].chargeType = 'MONTH';
		} else if (chargeType == 'B') {
			   	that.data.accountStorageQueryResult.items[initemsindex].chargeType = 'QUARTER';
		} else if (chargeType == 'C') {
			   	that.data.accountStorageQueryResult.items[initemsindex].chargeType = 'YEAR';
		}*/
   	
   	that.data.accountStorageQueryResult.items[initemsindex].chargeType = chargeType;
   	
   	that.setData({
   		accountStorageQueryResult : that.data.accountStorageQueryResult
   	});
   	
   	//改变之后，把数据传去后台，重新计算 总价	
   	app.getUserOpedId(function() {
   	  var changeStorageResult = that.data.accountStorageQueryResult.items[initemsindex];
   	  
   	  var params = {
  	  	method : 'wechat.wisen.account.storage.update',
  	  	openId : app.globalData.userInfo.openid,
  	  	data : {
  	  		accountCode : that.data.accountCode,
  	  		initems : [changeStorageResult]
  	  	} 		
  	  };
  	  
  	  netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  	  	onPre: function() {
		  		netUtil.showLoadingDialog(that);
		  	},
		  	onSuccess : function(resp) {
		  			
		  		//如果请求成功
		  		if(resp.ret == app.globalData.respSuccessCode) {
		  			
		  			//重新查询 订单数据并重新显示
		  			that.getAccountStorageQueryResult();
		  		
		  		} else {
		  			if(resp.message != undefined && resp.message != null && resp.message != '') {
 		  				netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 		  			} else {
 		  				netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 		  			}
		  		}
		  	},
		  	onError: function(msgCanShow, code, hiddenMsg) {
        	console.log("fail");
		  		netUtil.hideLoadingDialog(that);
		  		netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
		  	}
  	  }).send();
   	});
  },
  
   pay : function(e) {
  	var that = this;
  	
  	netUtil.showAlertDialog("提示", '确认支付?', true, "确定", null, function() {
  		var params = {
  			openId : app.globalData.userInfo.openid,
  			data : {
  				accountCode : that.data.accountCode
  			}
  		};
  	
  	netUtil.buildRequest(that,'/wisen/wechat/wxPay',params,{
  			onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				onSuccess : function(resp){
					
					netUtil.hideLoadingDialog(that);
					
					//如果请求成功
					if(resp.ret == app.globalData.respSuccessCode) {
						
						var data = resp.data;
						
						wx.requestPayment({
							'appId': data.appid,
  					  'timeStamp': data.timeStamp,
  					  'nonceStr': data.nonceStr,
   						'package': data.package,
   						'signType': 'MD5',
   						'paySign': data.paySign,
   						
   						'success':function(res){
   							
   							//如果支付成功，做响应处理
   							if(res.errMsg.indexOf('ok') >= 0 ) {
   								wx.navigateTo({
         						 url: '/pages/mypay/mypay'
     							 });
   							}
   							
  						},
  					  'fail':function(res){
  					  	
  					  	if(res.errMsg.indexOf('cancel') >= 0) {    //如果用户取消支付
  					  		netUtil.showAlertDialog("提示", "你已经取消支付", false, "确定", null, null);
  					  	} else {       //如果支付失败
  					  		 netUtil.showAlertDialog("提示", "支付失败", false, "确定", null, null);
  					  	}
  					  	
   						}
						})
						
					} else {
						if(resp.message != undefined && resp.message != null && resp.message != '') {
 							netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 						} else {
 							netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 						}
					}
				},
				onError: function(msgCanShow, code, hiddenMsg) {
       	  console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
  		}).send();
  	});
  	
  }

})